package io.renren.sf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.Constant;
import io.renren.common.utils.R;
import io.renren.sf.entity.*;
import io.renren.sf.service.*;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author cyl_txw
 * @email cyl_txw@gmail.com
 * @date 2021-03-31 13:04:11
 */
@RestController
@RequestMapping("sf/cbstiffsystem")
public class CbStiffSystemController {
    @Autowired
    private CbStiffSystemService cbStiffSystemService;

    @Autowired
    private CbStiffBookService cbStiffBookService;

    /**
     * 获取硬笔系统以及对应的课本
     * @return
     */
    @GetMapping("/getSystemsAndBooks")
    public R getSystemsAndBooks() {
        List<CbStiffSystemEntity> systems = cbStiffSystemService.list();
        if (systems != null){
            for (CbStiffSystemEntity system : systems){
                List<CbStiffBookEntity> books = cbStiffBookService.list(new QueryWrapper<CbStiffBookEntity>().eq("sid", system.getId()));
                system.setBooks(books);
            }
        }
        return R.ok().put("systems",systems);
    }

    /**
     * 硬笔系统分页
     */
    @GetMapping("/list")
    public R stiffSystemPage(@RequestParam Map<String, Object> params) {
        long page = Long.parseLong((String) params.get("page"));
        long limit = Long.parseLong((String) params.get("limit"));
        Page<CbStiffSystemEntity> stiffSystemPage = new Page<>(page, limit);
        cbStiffSystemService.page(stiffSystemPage);
        return R.ok().put("stiffSystemPage", stiffSystemPage);
    }
    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
        CbStiffSystemEntity systemEntity = cbStiffSystemService.getById(id);

        return R.ok().put("stiffSystem", systemEntity);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody CbStiffSystemEntity systemEntity){
        systemEntity.setCreatetime(new Date());
        cbStiffSystemService.save(systemEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody CbStiffSystemEntity systemEntity){
        cbStiffSystemService.updateById(systemEntity);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        cbStiffSystemService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
